const dbMod = require('../db/dbMod');
const async = require('async')
const replySQL = require('./replySQL')
const fs = require('fs')

function getidpost (userID, callback) {
  let sql1 = 'SELECT userName FROM user WHERE userID=?';
  dbMod.query(sql1, [userID], (result) => {
    var userName = eval(result)[0].userName;
    let sql2 = 'SELECT * FROM post WHERE postUserID=?';
    dbMod.query(sql2, [userID], (result) => {
      var posts = eval(result);
      callback(posts, userName)
    })
  })
}

function addpost (userID, title, content, partitionID, postTime, callback) {
  let sql1 = 'INSERT INTO post (postTitle, postContent, postPartID, postUserID, postRepCount, postTime, postReplyTime) VALUES (?,?,?,?,0,?,?)'
  dbMod.query(sql1, [title, content, partitionID, userID, postTime, postTime], (result) => {
    var result1 = result
    // 更新partition相关项的postNum
    let sql2 = 'UPDATE `partition` SET postNum=postNum+1 WHERE partitionID=?'
    dbMod.query(sql2, [partitionID], (result) => {
      let sql3 = 'SELECT * FROM post WHERE postID=?'
      dbMod.query(sql3, [result1.insertId], (result) => {
        var post = eval(result)[0]
        let sql4 = 'SELECT * FROM `partition` WHERE partitionID=?'
        dbMod.query(sql4, [partitionID], (result) => {
          var partition = eval(result)[0]
          callback(post, partition)
        })
      })
    })
  })
}

function getpost (partitionID, callback) {
  let sql1 = 'SELECT * FROM `partition` where partitionID = ?'
  dbMod.query(sql1, [partitionID], (result) => {
    var partitions = eval(result)
    let sql2 = 'SELECT * FROM post WHERE postPartID=? ORDER BY postReplyTime DESC'
    dbMod.query(sql2, [partitionID], (result) => {
      var posts = eval(result)
      callback(partitions, posts)
    })
  })
}

function delpost (postID, callback) {
  // select出所有reply
  let sql1 = 'SELECT * FROM reply WHERE replyPostID=? AND isAnnouncement=0'
  dbMod.query(sql1, [postID], (result) => {
    var replies = eval(result)

    // 删除所有replycontent文件
    var deleteFileTasks = []
    replies.forEach(reply => {
      function deleteFile (done) {
        fs.unlink(replySQL.dir + reply.replyContent, (err) => {
          if (err) {
            console.log(err)
            done(err, null)
          }
          else {
            console.log('deleted file: ' + reply.replyContent)
            done(null, null)
          }
        })
      }
      deleteFileTasks.push(deleteFile)
    });
    async.parallel(deleteFileTasks, (err, results) => {
      if (err) console.log(err)
      else {
        // 删除所有相关reply
        let sql2 = 'DELETE FROM reply WHERE replyPostID=? AND isAnnouncement=0'
        dbMod.query(sql2, [postID], (result) => {
          var result1 = eval(result)

          // 删除主帖replycontent文件
          let sql3 = 'SELECT * FROM post WHERE postID=?'
          dbMod.query(sql3, [postID], (result) => {
            var filename = eval(result)[0].postContent
            var partitionID = eval(result)[0].postPartID
            fs.unlink(replySQL.dir + filename, (err) => {
              if (err) console.log(err)
              else {
                console.log('deleted file: ' + filename)
              }

              // 分区post数-1
              let sql4 = 'UPDATE `partition` SET postNum=postNum-1 WHERE partitionID=?'
              dbMod.query(sql4, [partitionID], (result) => {
                var result2 = eval(result)

                // 删除主帖
                let sql5 = 'DELETE FROM post WHERE postID=?'
                dbMod.query(sql5, [postID], (result) => {
                  callback()
                })
              })
            })
          })
        })
      }
    })
  })
}

exports.getidpost = getidpost
exports.addpost = addpost
exports.getpost = getpost
exports.delpost = delpost